package org.budo.netty.servlet.server.handler.servlet.registration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;

/**
 * @author lmw
 */
public class BudoFilterRegistration implements FilterRegistration.Dynamic {
    private String filterName;

    private String className;

    private ServletContext servletContext;

    private Map<String, String> initParameters = new HashMap<String, String>();

    private List<BudoFilterMapping> filterMappings = new ArrayList<BudoFilterMapping>();

    public BudoFilterRegistration(String filterName, String className, ServletContext servletContext) {
        this.filterName = filterName;
        this.className = className;
        this.servletContext = servletContext;
    }

    public ServletContext getServletContext() {
        return this.servletContext;
    }

    @Override
    public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... urlPatterns) {
        this.filterMappings.add(new BudoFilterMapping(dispatcherTypes.toArray(new DispatcherType[0]), urlPatterns));
    }

    @Override
    public Collection<String> getUrlPatternMappings() {
        List<String> urlPatternMappings = new ArrayList<String>();

        for (BudoFilterMapping filterMapping : this.filterMappings) {
            List<String> list = Arrays.asList(filterMapping.getUrlPatterns());
            urlPatternMappings.addAll(list);
        }

        return urlPatternMappings;
    }

    @Override
    public String getClassName() {
        return this.className;
    }

    /**
     * 返回重复的
     */
    @Override
    public Set<String> setInitParameters(Map<String, String> initParameters) {
        this.initParameters = initParameters;
        return null;
    }

    @Override
    public boolean setInitParameter(String name, String value) {
        this.initParameters.put(name, value);
        return true;
    }

    @Override
    public String getInitParameter(String name) {
        return this.initParameters.get(name);
    }

    @Override
    public String getName() {
        return this.filterName;
    }

    public List<BudoFilterMapping> getFilterMappings() {
        return this.filterMappings;
    }

    @Override
    public Map<String, String> getInitParameters() {
        return this.initParameters;
    }

    @Override
    public void addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Collection<String> getServletNameMappings() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void setAsyncSupported(boolean isAsyncSupported) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String toString() {
        return super.toString() + ", name=" + this.getName() + ", className=" + this.getClassName();
    }
}